package com.etogc.groupchat.group.biz;

import android.app.Activity;

import com.etogc.groupchat.group.adpt.MyGroupAdapter;
import com.etogc.http.ReqDataHandler;
import com.etogc.http.ReqErrorHandler;
import com.etogc.http.VolleyHttpUtil;
import com.etogc.groupchat.common.CommonSp;
import com.etogc.util.GcUrl;
import com.etogc.util.IntentHelper;
import com.etogc.util.YcToastHelper;
import com.etogc.view.LoadDialog;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ZeroVoid on 2018-2-28.
 */
public class GroupHttpBiz {

    public static void addFavGroup(String groupId) {
        VolleyHttpUtil.getInstance().getWithCallback(GcUrl.ADD_FAV_GROUP + groupId, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                boolean isSuccess = ReqErrorHandler.getInstance().isSuccess(response, true);
                if (isSuccess) {
                    Map<String, Object> map = ReqDataHandler.getMapWithEntityNoGson(response);
                    if (map != null) {
                        String isCoOk = (String) map.get("is_collect");
                        if ("1".equals(isCoOk)) {
                            YcToastHelper.getInstance()._toast("收藏成功");
                        } else {
                            YcToastHelper.getInstance()._toast("取消收藏成功");
                        }
                    } else {
                        YcToastHelper.getInstance()._toast("收藏失败");
                    }
                }
            }
        });
    }

    public static void getFavGroup(final Activity act, final MyGroupAdapter adapter) {
        LoadDialog.show(act);
        HashMap<String, Object> map = new HashMap<>();
        //TODO 暂时不做分页,写死
//        map.put("showCount", "100");
//        map.put("currentPage", "1");
        VolleyHttpUtil.getInstance().postWithCallback(GcUrl.FAV_GROUP_LIST, null, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                LoadDialog.dismiss(act);
                boolean isSuccess = ReqErrorHandler.getInstance().isSuccess(response, true);
                if (isSuccess) {
                    Map<String, Object> map = ReqDataHandler.getMapWithEntityNoGson(response);
                    if (map != null) {
                        List<HashMap<String, String>> list = (List<HashMap<String, String>>) map.get("list");
                        if (list != null && list.size() > 0) {
                            adapter.setData(list);
                        } else {
                            YcToastHelper.getInstance()._toast("暂无数据");
                        }
                    } else {
                        YcToastHelper.getInstance()._toast("暂无数据");
                    }
                }
            }
        });
    }

    public static void getMyGroup(final Activity act, final MyGroupAdapter adapter) {
        LoadDialog.show(act);
        VolleyHttpUtil.getInstance().postWithCallback(GcUrl.MY_GROUP_LIST, null, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                LoadDialog.dismiss(act);
                boolean isSuccess = ReqErrorHandler.getInstance().isSuccess(response, true);
                if (isSuccess) {
                    Map<String, Object> map = ReqDataHandler.getMapWithEntityNoGson(response);
                    if (map != null) {
                        List<HashMap<String, String>> list = (List<HashMap<String, String>>) map.get("list");
                        if (list != null && list.size() > 0) {
                            adapter.setData(list);
                            adapter.notifyDataSetChanged();
                        } else {
                            YcToastHelper.getInstance()._toast("暂无数据");
                        }
                    } else {
                        YcToastHelper.getInstance()._toast("暂无数据");
                    }
                }
            }
        });
    }


    /**
     * 获取群信息
     *
     * @param act
     * @param callback
     */
    public static void getGroupInfo(final Activity act, String group_id, VolleyHttpUtil.ResponseCallBack callback) {
        LoadDialog.show(act);
        String url = GcUrl.GET_GROUP_INFO + "/" + group_id;
        VolleyHttpUtil.getInstance().getWithCallback(url, callback);
    }


    public static void createGroup(final Activity act, String groupName, String remark, String type) {
        LoadDialog.show(act);
        HashMap<String, Object> map = new HashMap<>();
        map.put("group_name", groupName);
        map.put("remark", remark);
        map.put("count", type);
        VolleyHttpUtil.getInstance().postWithCallback(GcUrl.CREATE_GROUP, map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                LoadDialog.dismiss(act);
                boolean isSuccess = ReqErrorHandler.getInstance().isSuccess(response, true);
                if (isSuccess) {
                    IntentHelper.finish(act);
                    YcToastHelper.getInstance()._toast("申请成功");
                }
            }
        });
    }

    public static void joinGroup(final Activity act, String groupID, String inviteCode) {
        LoadDialog.show(act);
        HashMap<String, Object> map = new HashMap<>();
        map.put("group_id", groupID);
        map.put("invite_code", inviteCode);
        map.put("user_id", CommonSp.getUserID());
        VolleyHttpUtil.getInstance().postWithCallback(GcUrl.JOIN_GROUP, map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                LoadDialog.dismiss(act);
                boolean isSuccess = ReqErrorHandler.getInstance().isSuccess(response, true);
                if (isSuccess) {
                    IntentHelper.finish(act);
                    YcToastHelper.getInstance()._toast("恭喜您申请成功,请等候审核");
                }
            }
        });

    }


}
